﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;

namespace DCSLibrary
{
    public class ContactUI
    {
        private const String CACHE_KEY = "Contact_GetAll";

        public int Add(int CategoryPageID, String ContactName, String ContactAlias, String ContactImage,
            String ContactCompanyName, String ContactWebsite, String ContactEmail, String ContactTelephone, String ContactMobilephone, 
            String ContactYahoo, String ContactSkype,String ContactCompanyAddress, String ContactTaxCode ,String ContactFax,
            String ContactAbstract, String ContactContent, String ContactPageTitle, String MetaKeyword, String MetaDescription,
            int ViewOrder, bool IsTypical, bool IsApproved)
        {

            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Contact_Add", CategoryPageID, ContactName, ContactAlias, ContactImage,
                                        ContactCompanyName, ContactWebsite, ContactEmail, ContactTelephone, ContactMobilephone, ContactYahoo, ContactSkype, ContactCompanyAddress, ContactTaxCode,
                                            ContactFax, ContactAbstract, ContactContent, ContactPageTitle,
                                        MetaKeyword, MetaDescription, ViewOrder, IsTypical, IsApproved);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int ContactID, int CategoryPageID, String ContactName, String ContactAlias, String ContactImage,
            String ContactCompanyName, String ContactWebsite, String ContactEmail, String ContactTelephone, String ContactMobilephone, 
            String ContactYahoo, String ContactSkype,String ContactCompanyAddress, String ContactTaxCode, String ContactFax,
            String ContactAbstract, String ContactContent, String ContactPageTitle, String MetaKeyword, String MetaDescription,
            int ViewOrder, bool IsTypical, bool IsApproved)
        {

            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Contact_update", ContactID, CategoryPageID, ContactName, ContactAlias, ContactImage,
                                        ContactCompanyName, ContactWebsite, ContactEmail, ContactTelephone, ContactMobilephone, ContactYahoo, ContactSkype, ContactCompanyAddress, ContactTaxCode,
                                            ContactFax, ContactAbstract, ContactContent, ContactPageTitle,
                                        MetaKeyword, MetaDescription, ViewOrder, IsTypical, IsApproved);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + ContactID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Contact_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int ContactID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + ContactID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Contact_get", ContactID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + ContactID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int ContactID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Contact_delete", ContactID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
